package LC;

import java.util.Stack;

/**
 * @ClassName KthNode
 * @Description: TODO
 * @Author hl
 * @Date 2020/6/8
 * @Version V1.0
 **/
public class KthNode {
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        if(pRoot==null||k==0){
            return null;
        }
        TreeNode node=pRoot;
        Stack<TreeNode> stack=new Stack<>();
        int count=0;
        while(node!=null||!stack.isEmpty()){
            if(node!=null){
                stack.push(node);
                node=node.left;
            }else{
                node=stack.pop();
                count++;
                if(count==k){
                    return  node;
                }
                node=node.right;
            }
        }
        return  null;
    }
}
